<template>
  <el-dialog title="物料产品选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="80%"
    center
  >
    <el-row :gutter="20">
      <!--物料数据-->
      <el-col :span="24" :xs="24">
        <el-table  border ref="itemTable" v-loading="loading" :data="itemList"  @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
          <el-table-column width="50" align="center" >
            <template v-slot="scope">
              <el-radio v-model="selectedItemId" :label="scope.row.itemId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
            </template>
          </el-table-column>

          <el-table-column label="物料编码" width = "120" align="center" key="itemCode" prop="itemCode" v-if="columns[0].visible" >
          </el-table-column>
          <el-table-column label="物料名称" min-width="120" align="left" key="itemName" prop="itemName" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="规格型号" align="left" key="specification" prop="specification" v-if="columns[2].visible" :show-overflow-tooltip="true" />
          <el-table-column label="单位" align="center" key="unitOfMeasure" prop="unitOfMeasure" v-if="columns[3].visible" :show-overflow-tooltip="true" />
          <el-table-column label="所属分类" align="center" key="itemTypeName" prop="itemTypeName" v-if="columns[4].visible" width="120" />
          <el-table-column label="采购数量" align="center" prop="quantity" v-if="columns[5].visible" width="160"/>
          <el-table-column label="已采购数量" align="center" prop="purchasedQuantity" v-if="columns[6].visible" width="160"/>
          <el-table-column label="图号" align="center" prop="drawNumber" width="80" v-if="columns[7].visible"/>
        </el-table>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getItemByPlanId} from "@/api/mes/cg/plan";


export default {
  name: "CgItemSingle",
  dicts: ['sys_yes_no','mes_item_product'],
  props: {
    planId:null
  },
  data() {
    return {
      showFlag:false,
      selectedItemId: undefined,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      // 列信息
      columns: [
        { key: 0, label: `物料/产品编码`, visible: true },
        { key: 1, label: `物料/产品名称`, visible: true },
        { key: 2, label: `规格型号`, visible: true },
        { key: 3, label: `单位`, visible: true },
        { key: 4, label: `物料分类`, visible: true },
        { key: 5, label: `采购数量`, visible: true },
        { key: 6, label: `已采购数量`, visible: true },
        { key: 7, label: `图号`, visible: true },
      ]
    };
  },
  created() {
    /*this.getList();*/
  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      getItemByPlanId(this.planId).then(response => {
          this.itemList = response.data;
          this.loading = false;
        }
      );
    },
    handleCurrent(row){
      if(row){
        this.selectedRows = row;
        this.selectedItemId = row.itemId;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect(){
        if(this.selectedRows==null || this.selectedRows.length==0){
            this.$notify({
                title:'提示',
                type:'warning',
                message: '请至少选择一条数据!'
            });
            return;
        }
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
    }

  },
};
</script>
